package com.accessibleworlds.report;

import java.util.ArrayList;

import org.json.JSONArray;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.accessibleworlds.R;
import com.accessibleworlds.user.GuestUserPage;
import com.accessibleworlds.util.Connections;
import com.accessibleworlds.util.SharedVariables;

public class ReportView extends Activity implements OnClickListener {

	private TextView title,description,type,status, reportRate, reportPlusUndo, reportMinusUndo;
	private ListView comments;
	private Button viewImages,viewVideos,edit, addComment, plusOne, minusOne, plus, minus, editComment;
	private Bundle bundle;
	private int addReportRate,rate, commentRate, pos;
	private String reportID, userID, commentText, email;
	private ArrayList<Comment> comment;
	private CustomListAdapter customAdapt;
	private Dialog commentDialog;
	private Dialog videoDialog;
	private EditText addCommentText;
	private ProgressDialog progressDialog;
	private SharedPreferences preferences;
	private Comment token;
	private String [] videoList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report_view);

		InitializeVariables();
		reportID = bundle.getString("reportID");
		userID = preferences.getString("userID", "");
		if(userID.equals(""))
			userID = "-1";
		System.out.println("userID: "+userID);
		email = preferences.getString("email", "");
		if(email.equals(""))
			email = "Anonymous";
		rate = Integer.parseInt(bundle.getString("rate"));
		reportRate.setText(rate+"");
		if(userID.equals("-1")){
			plusOne.setVisibility(View.GONE);
			minusOne.setVisibility(View.GONE);
			reportPlusUndo.setVisibility(View.GONE);
			reportMinusUndo.setVisibility(View.GONE);
			edit.setVisibility(View.GONE);
		}
		else
			new GetUserReportRate().execute();
		new GetComments().execute();
	}


	private void InitializeVariables(){
		title= (TextView) findViewById(R.id.reportViewTitle);
		description= (TextView) findViewById(R.id.reportViewDescription);
		type= (TextView) findViewById(R.id.ReportViewType);
		status= (TextView) findViewById(R.id.reportViewStatus);
		reportRate = (TextView) findViewById(R.id.reportRating);
		reportPlusUndo = (TextView) findViewById(R.id.reportPlusOneUndo);
		reportMinusUndo = (TextView) findViewById(R.id.reportMinusOneUndo);

		bundle = getIntent().getExtras();

		title.setText(bundle.getString("title"));
		description.setText(bundle.getString("description"));
		int vioType = Integer.parseInt(bundle.getString("type"));
		if(vioType==1)
			type.setText("VIOLATION TYPE: HOLE");
		else if(vioType==2)
			type.setText("VIOLATION TYPE: NO PEDESTRIAN ROAD");
		else if(vioType==3)
			type.setText("VIOLATION TYPE: NO WHEEL CHAIR RAMP");
		else if(vioType==4)
			type.setText("VIOLATION TYPE: NO ELEVATOR");
		else if(vioType==5)
			type.setText("VIOLATION TYPE: NO WAY ACROSS THE STREET");
		else if(vioType==6)
			type.setText("VIOLATION TYPE: RECONSTRUCTION OF THE ROAD");
		else if(vioType==7)
			type.setText("VIOLATION TYPE: UNSAFE SIGNBOARD");
		else if(vioType==8)
			type.setText("VIOLATION TYPE: BROKEN PEDESTRIAN ROAD");
		else
			type.setText("VIOLATION TYPE: OTHER");

		status.setText("STATUS: "+bundle.getString("status").toUpperCase());

		edit=(Button)findViewById(R.id.reportViewEdit);
		viewImages = (Button)findViewById(R.id.reportViewImages);
		viewVideos = (Button)findViewById(R.id.reportViewVideos);
		addComment = (Button)findViewById(R.id.reportViewAddComment);
		plusOne = (Button)findViewById(R.id.reportViewPlusOne);
		minusOne = (Button)findViewById(R.id.reportViewMinusOne);

		edit.setOnClickListener(this);
		viewImages.setOnClickListener(this);
		viewVideos.setOnClickListener(this);
		addComment.setOnClickListener(this);
		plusOne.setOnClickListener(this);
		minusOne.setOnClickListener(this);
		reportPlusUndo.setOnClickListener(this);
		reportMinusUndo.setOnClickListener(this);

		comments = (ListView)findViewById(R.id.ReportViewComments);
		comment = new ArrayList<Comment>();

		progressDialog = new ProgressDialog(ReportView.this);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);

		preferences = getSharedPreferences(SharedVariables.fileName, MODE_PRIVATE);
	}

	public class GetUserReportRate extends AsyncTask<Void, Integer, Integer>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected Integer doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.getReportRate;
					String [] parameters = {"accessCode","userID","reportID"};
					String [] paramaterValues = {SharedVariables.accessCode, userID, reportID};
					takenData = Connections.connection(url,parameters,paramaterValues);
					String first = takenData[0].substring(0, 2);
					if("OK".equalsIgnoreCase(first)) {
						publishProgress(1);
						return Integer.parseInt(takenData[0].substring(2));
					}
					else {
						publishProgress(2);
						return -2;
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return -2;
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetComments().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			progressDialog.dismiss();
		}

		@Override
		protected void onPostExecute(Integer result) {
			if(result != -2){
				if(result == 0){
					reportPlusUndo.setVisibility(View.GONE);
					reportMinusUndo.setVisibility(View.GONE);
				}
				else if(result == 1){
					reportMinusUndo.setVisibility(View.GONE);
					reportPlusUndo.setVisibility(View.VISIBLE);
					reportPlusUndo.setClickable(true);
					plusOne.setVisibility(View.GONE);
					minusOne.setVisibility(View.VISIBLE);
				}
				else{
					reportMinusUndo.setVisibility(View.VISIBLE);
					reportPlusUndo.setVisibility(View.GONE);
					reportMinusUndo.setClickable(true);
					minusOne.setVisibility(View.GONE);
					plusOne.setVisibility(View.VISIBLE);
				}
			}
		}
	}

	public class GetComments extends AsyncTask<Void, Integer, ArrayList<Comment>>{

		private String[] takenData = null;
		private ArrayList<Comment> recievedComments = new ArrayList<Comment>();

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected ArrayList<Comment> doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.getComments;
					String [] parameters = {"accessCode","reportID"};
					String [] paramaterValues = {SharedVariables.accessCode, reportID};

					takenData = Connections.connection(url,parameters,paramaterValues);
					if("OK".equalsIgnoreCase(takenData[0])) {
						for(int i=1;i<takenData.length;i++){
							JSONArray token = new JSONArray(takenData[i]);
							recievedComments.add(new Comment(Integer.parseInt(token.getString(0)), token.getString(1), token.getString(2), Integer.parseInt(token.getString(3))));
						}
						publishProgress(1);
						return recievedComments;
					}
					else {
						publishProgress(2);
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return recievedComments;
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetComments().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			progressDialog.dismiss();
		}

		@Override
		protected void onPostExecute(ArrayList<Comment> result) {
			comment = result;
			customAdapt = new CustomListAdapter(ReportView.this, R.layout.chat_list_item, comment);
			comments.setAdapter(customAdapt);
			comments.setOnItemLongClickListener(rateComment);
			comments.setBackgroundColor(getResources().getColor(R.color.white));
			comments.setCacheColorHint(getResources().getColor(R.color.white));
		}
	}

	//Joining a channel
	OnItemLongClickListener rateComment = new OnItemLongClickListener() {
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
			pos = position;
			token = customAdapt.getItem(pos);
			if(userID.equals("-1")){
				commentDialog = new Dialog(ReportView.this);
				commentDialog.setTitle("Rating: "+token.rate);
				commentDialog.setContentView(R.layout.pop_up_with_three_button);
				
				plus = (Button) commentDialog.findViewById(R.id.threebuttons_choice1);
				minus = (Button) commentDialog.findViewById(R.id.threebuttons_choice2);
				editComment = (Button) commentDialog.findViewById(R.id.threebuttons_choice3);
				plus.setVisibility(View.GONE);
				minus.setVisibility(View.GONE);
				editComment.setVisibility(View.GONE);
				
				commentDialog.show();
			}
			else
				new GetUserCommentRate().execute();
			return false;
		}
	};

	public class GetUserCommentRate extends AsyncTask<Void, Integer, Integer>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected Integer doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.getCommentRate;
					String [] parameters = {"accessCode","userID","commentID"};
					System.out.println("userID: "+userID+" commentID: "+token.commentID);
					String [] paramaterValues = {SharedVariables.accessCode, userID, token.commentID+""};
					takenData = Connections.connection(url,parameters,paramaterValues);
					String first = takenData[0].substring(0, 2);
					if("OK".equalsIgnoreCase(first)) {
						publishProgress(1);
						return Integer.parseInt(takenData[0].substring(2));
					}
					else {
						publishProgress(2);
						return -2;
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return -2;
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetComments().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			progressDialog.dismiss();
		}

		@Override
		protected void onPostExecute(Integer result) {
			commentDialog = new Dialog(ReportView.this);
			commentDialog.setTitle("Rating: "+token.rate);
			commentDialog.setContentView(R.layout.pop_up_with_three_button);

			plus = (Button) commentDialog.findViewById(R.id.threebuttons_choice1);
			minus = (Button) commentDialog.findViewById(R.id.threebuttons_choice2);
			editComment = (Button) commentDialog.findViewById(R.id.threebuttons_choice3);
			editComment.setText("Edit Comment");
			if(!token.user.equalsIgnoreCase(email)){
				editComment.setVisibility(View.GONE);
			}
			if(result != -2){
				if(result == 0){
					plus.setText("+1");
					minus.setText("-1");
				}
				else if(result == 1){
					plus.setText("Undo");
					minus.setText("-1");
				}
				else{
					plus.setText("+1");
					minus.setText("Undo");
				}
			}
			commentDialog.show();
			
			plus.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if(plus.getText().toString().equalsIgnoreCase("+1")){
						commentRate = 1;
					}else{
						commentRate = 2;
					}
					new rateComment().execute();
				}
			});
			minus.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if(minus.getText().toString().equalsIgnoreCase("-1")){
						commentRate = -1;
					}else{
						commentRate = -2;
					}
					new rateComment().execute();
				}
			});
			editComment.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					commentDialog.dismiss();
					commentDialog = new Dialog(ReportView.this);
					commentDialog.setTitle("Add Comment");
					commentDialog.setContentView(R.layout.comment_pop_up);
					addCommentText = (EditText) commentDialog.findViewById(R.id.addComment);
					addCommentText.setText(token.comment);
					Button editCommentButton = (Button) commentDialog.findViewById(R.id.addCommentButton);
					editCommentButton.setText("Edit Comment");
					commentDialog.show();

					editCommentButton.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							commentText = addCommentText.getText().toString();
							new EditComment().execute();
							commentDialog.cancel();
						}
					});
				}
			});
		}
	}
	
	public class EditComment extends AsyncTask<Void, Integer, Boolean>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.updateComment;
					String [] parameters = {"accessCode","commentID","comment"};
					String [] paramaterValues = {SharedVariables.accessCode, token.commentID+"", commentText};

					takenData = Connections.connection(url,parameters,paramaterValues);
					if("OK".equalsIgnoreCase(takenData[0])) {
						System.out.println("girdim");
						publishProgress(1);
						return true;
					}
					else {
						publishProgress(2);
						return false;
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return false;
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetComments().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			progressDialog.dismiss();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if(result){
				token.comment = commentText;
				customAdapt.getItem(pos).comment = commentText;
			}
		}
	}
	
	public class rateComment extends AsyncTask<Void, Integer, Boolean>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.addCommentRate;
					String [] parameters = {"accessCode","userID","commentID","rating"};
					String[] paramaterValues = {SharedVariables.accessCode, userID, token.commentID+"", commentRate+""};
					if(commentRate == 2 || commentRate == -2)
						paramaterValues[3] = "0";
					takenData = Connections.connection(url,parameters,paramaterValues);
					if("OK".equalsIgnoreCase(takenData[0])) {
						System.out.println("commentRate: "+commentRate);
						publishProgress(1);
						return true;
					}
					else {
						publishProgress(2);
						return false;
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return false;
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetComments().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			progressDialog.dismiss();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if(result){
				if(commentRate == 1){
					if(minus.getText().toString().equalsIgnoreCase("Undo"))
						token.rate+=2;
					else
						token.rate++;
					commentDialog.setTitle("Rate: "+token.rate);
					plus.setText("Undo");
					minus.setText("-1");
				}
				else if(commentRate == -1){
					if(plus.getText().toString().equalsIgnoreCase("Undo"))
						token.rate-=2;
					else
						token.rate--;
					commentDialog.setTitle("Rate: "+token.rate);
					minus.setText("Undo");
					plus.setText("+1");
				}
				else if(commentRate == 2){
					token.rate--;
					commentDialog.setTitle("Rate: "+token.rate);
					plus.setText("+1");
					minus.setText("-1");
				}
				else{
					token.rate++;
					commentDialog.setTitle("Rate: "+token.rate);
					plus.setText("+1");
					minus.setText("-1");				
				}
			}
		}
	}
	
	@Override
	public void onBackPressed() {
		Intent intent=null;
		Bundle bundleSend = null;
		if(userID.equals("-1"))
			intent = new Intent(this,GuestUserPage.class);
		else if(bundle.getString("previousID").equalsIgnoreCase(SharedVariables.searchReportID)){
			intent = new Intent(this,SearchReport.class);
			bundleSend = new Bundle();
			bundleSend.putString("previousID", SharedVariables.reportViewID);
			intent.putExtras(bundleSend);
		}
		else if(bundle.getString("previousID").equalsIgnoreCase(SharedVariables.editOldReportsID)){
			intent = new Intent(this,EditOldReports.class);
			bundleSend = new Bundle();
			bundleSend.putString("previousID", SharedVariables.reportViewID);
			intent.putExtras(bundleSend);
		}
		startActivity(intent);
		overridePendingTransition(R.anim.fade, R.anim.hold);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.mainmenu, menu);
		return true;
	}

	public void onClick(View v) {
		if(v.getId()==edit.getId()){
			Intent intent = new Intent(ReportView.this,EditReport.class);
			Bundle bundleSend = new Bundle();
			bundleSend.putString("previousID", bundle.getString("previousID"));
			bundleSend.putString("reportID", reportID);
			bundleSend.putString("title", bundle.getString("title"));
			bundleSend.putString("description", bundle.getString("description"));
			bundleSend.putString("type", bundle.getString("type"));
			bundleSend.putString("status", bundle.getString("status"));
			System.out.println(bundle.getString("status")+" "+bundle.getString("type"));
			bundleSend.putString("latitude", bundle.getString("latitude"));
			bundleSend.putString("longitude", bundle.getString("longitude"));
			bundleSend.putString("rate", ""+rate);
			intent.putExtras(bundleSend);
			startActivity(intent);
			overridePendingTransition(R.anim.fade, R.anim.hold);
			finish();
		}
		else if(v.getId()==viewImages.getId()){
			new GetImageTask().execute();
		}
		else if(v.getId()==viewVideos.getId()){
			new GetVideoTask().execute();
		}
		else if(v.getId()==addComment.getId()){
			commentDialog = new Dialog(ReportView.this);
			commentDialog.setTitle("Add Comment");
			commentDialog.setContentView(R.layout.comment_pop_up);

			addCommentText = (EditText) commentDialog.findViewById(R.id.addComment);
			Button addCommentButton = (Button) commentDialog.findViewById(R.id.addCommentButton);
			addCommentButton.setText("Add Comment");
			commentDialog.show();

			addCommentButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					commentText = addCommentText.getText().toString();
					new AddComment().execute();
					commentDialog.cancel();
				}
			});
		}
		else if(v.getId()==plusOne.getId()){
			addReportRate = 1;
			new rateReport().execute();
		}
		else if(v.getId()==minusOne.getId()){
			addReportRate = -1;
			new rateReport().execute();
		}
		else if(v.getId()==reportPlusUndo.getId()){
			addReportRate = 2;
			new rateReport().execute();
		}
		else if(v.getId()==reportMinusUndo.getId()){
			addReportRate = -2;
			new rateReport().execute();
		}
	}

	public class rateReport extends AsyncTask<Void, Integer, Boolean>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.addReportRate;
					String [] parameters = {"accessCode","userID","reportID","rating"};
					String[] paramaterValues = {SharedVariables.accessCode, userID, reportID, addReportRate+""};
					if(addReportRate == 2 || addReportRate == -2)
						paramaterValues[3] = "0";
					takenData = Connections.connection(url,parameters,paramaterValues);
					System.out.println("length "+takenData.length);
					if("OK".equalsIgnoreCase(takenData[0])) {
						System.out.println("girdim");
						publishProgress(1);
						return true;
					}
					else {
						publishProgress(2);
						return false;
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return false;
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetComments().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			progressDialog.dismiss();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if(result){
				if(addReportRate == 1){
					if(minusOne.getVisibility()==View.GONE)
						rate += 2;
					else
						rate++;
					reportRate.setText(rate+"");
					plusOne.setVisibility(View.GONE);
					reportPlusUndo.setVisibility(View.VISIBLE);
					minusOne.setVisibility(View.VISIBLE);
					reportMinusUndo.setVisibility(View.GONE);
				}
				else if(addReportRate == -1){
					if(plusOne.getVisibility() == View.GONE)
						rate-=2;
					else
						rate--;
					reportRate.setText(rate+"");
					minusOne.setVisibility(View.GONE);
					reportMinusUndo.setVisibility(View.VISIBLE);
					plusOne.setVisibility(View.VISIBLE);
					reportPlusUndo.setVisibility(View.GONE);
				}
				else if(addReportRate == 2){
					rate--;
					reportRate.setText(rate+"");
					reportPlusUndo.setVisibility(View.GONE);
					plusOne.setVisibility(View.VISIBLE);
					minusOne.setVisibility(View.VISIBLE);
					reportMinusUndo.setVisibility(View.GONE);
				}
				else{
					rate++;
					reportRate.setText(rate+"");
					reportMinusUndo.setVisibility(View.GONE);
					minusOne.setVisibility(View.VISIBLE);
					plusOne.setVisibility(View.VISIBLE);
					reportPlusUndo.setVisibility(View.GONE);				
				}
			}
		}
	}


	public class AddComment extends AsyncTask<Void, Integer, Boolean>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {

			if(Connections.serviceStatus()) {

				try {
					String url = Connections.URI+Connections.addComment;
					String [] parameters = {"accessCode","userID","reportID","comment"};
					String [] paramaterValues = {SharedVariables.accessCode, userID, reportID, commentText};

					takenData = Connections.connection(url,parameters,paramaterValues);
					if("OK".equalsIgnoreCase(takenData[0])) {
						System.out.println("girdim");
						publishProgress(1);
						return true;
					}
					else {
						publishProgress(2);
						return false;
					}
				}
				catch (Exception ex) {
					publishProgress(2);
				}
			}
			else {
				publishProgress(0);
			}
			return false;
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetComments().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			progressDialog.dismiss();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if(result){
				comment.add(new Comment(Integer.parseInt(reportID), email, commentText, 0));
				System.out.println("commentText: "+commentText);
			}
		}
	}

	public class GetImageTask extends AsyncTask<Void, Integer, String>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {
			if(Connections.serviceStatus()) {
				try {
					String url = Connections.URI+Connections.getImageList;
					String [] parameters = {"accessCode","reportID"};
					String [] paramaterValues = {SharedVariables.accessCode,reportID};

					takenData = Connections.connection(url,parameters,paramaterValues);

					if("OK".equalsIgnoreCase(takenData[0])) {
						if(takenData.length == 1) {
							publishProgress(3);
							return "OK";
						}
						else {
							for (int i=1; i <takenData.length;i++) {
								try {
									if(!"OK".equalsIgnoreCase(Connections.getImage(takenData[i]))) {
										i--;
									}
								}
								catch (Exception ex) {
									i--;
								}
							}
							publishProgress(2);
							return "OK";
						}
					}
					else {
						publishProgress(1);
					}
				}
				catch (Exception ex) {
					publishProgress(0);
				}
			}
			return "Fail";
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				progressDialog.dismiss();
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetImageTask().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			else if(progress[0]==1) {
				progressDialog.dismiss();
				Toast.makeText(ReportView.this, getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
			else if(progress[0]==2) {
				progressDialog.dismiss();
				Intent intent = new Intent(ReportView.this,ReportImageSwitcher.class);
				Bundle bundleSend = new Bundle();
				bundleSend.putString("previousID", bundle.getString("previousID"));
				bundleSend.putString("reportID", ""+bundle.getString("reportID"));
				bundleSend.putString("status", bundle.getString("status"));
				bundleSend.putString("type", ""+bundle.getString("type"));
				bundleSend.putString("title", bundle.getString("title"));
				bundleSend.putString("description", bundle.getString("description"));
				bundleSend.putString("latitude", ""+bundle.getString("latitude"));
				bundleSend.putString("longitude", ""+bundle.getString("longitude"));
				bundleSend.putString("rate", reportRate.getText().toString());
				String [] imageIDList = new String [takenData.length-1];
				for(int i=1;i<takenData.length;i++) {
					imageIDList[i-1] = takenData[i];
				}
				bundleSend.putStringArray("imageIDList", imageIDList);
				intent.putExtras(bundleSend);
				startActivity(intent);
				overridePendingTransition(R.anim.fade, R.anim.hold);
				finish();				
			}
			else if(progress[0]==3) {
				progressDialog.dismiss();
				Toast.makeText(ReportView.this, getResources().getStringArray(R.array.noImage)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
		}

		@Override
		protected void onPostExecute(String result) {}
	}
	
	public class GetVideoTask extends AsyncTask<Void, Integer, String>{

		private String[] takenData = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {
			if(Connections.serviceStatus()) {
				try {
					String url = Connections.URI+Connections.getVideoList;
					String [] parameters = {"accessCode","reportID"};
					String [] paramaterValues = {SharedVariables.accessCode,reportID};

					takenData = Connections.connection(url,parameters,paramaterValues);

					if("OK".equalsIgnoreCase(takenData[0])) {
						if(takenData.length == 1) {
							publishProgress(3);
							return "OK";
						}
						else {
							for (int i=1; i <takenData.length;i++) {
								try {
									if(!"OK".equalsIgnoreCase(Connections.getVideo(takenData[i]))) {
										i--;
									}
								}
								catch (Exception ex) {
									i--;
								}
							}
							publishProgress(2);
							return "OK";
						}
					}
					else {
						publishProgress(1);
					}
				}
				catch (Exception ex) {
					publishProgress(0);
				}
			}
			return "Fail";
		}

		protected void onProgressUpdate(Integer... progress) {
			if(progress[0] == 0) {
				progressDialog.dismiss();
				AlertDialog alertDialog = new AlertDialog.Builder(ReportView.this).create();
				alertDialog.setTitle(getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage]);
				alertDialog.setMessage(getResources().getStringArray(R.array.connectionProblemException)[SharedVariables.currentLanguage]);
				alertDialog.setButton(getResources().getStringArray(R.array.tryAgain)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						new GetImageTask().execute();
					}
				});
				alertDialog.setButton2(getResources().getStringArray(R.array.exit)[SharedVariables.currentLanguage], new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
				alertDialog.show();
			}
			else if(progress[0]==1) {
				progressDialog.dismiss();
				Toast.makeText(ReportView.this, getResources().getStringArray(R.array.connectionProblem)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
			else if(progress[0]==2) {
				videoDialog = new Dialog(ReportView.this);
				
				videoList = new String [takenData.length-1];
				for(int i=1;i<takenData.length;i++) {
					videoList[i-1] = takenData[i];
				}
				ListView listView = new ListView(ReportView.this);
				
				ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ReportView.this, android.R.layout.simple_list_item_1, videoList);
				listView.setAdapter(arrayAdapter);
				listView.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
						Intent intent = new Intent(ReportView.this,ReportVideoViewer.class);
						Bundle bundleSend = new Bundle();
						bundleSend.putString("previousID", bundle.getString("previousID"));
						bundleSend.putString("reportID", ""+bundle.getString("reportID"));
						bundleSend.putString("status", bundle.getString("status"));
						bundleSend.putString("type", ""+bundle.getString("type"));
						bundleSend.putString("title", bundle.getString("title"));
						bundleSend.putString("description", bundle.getString("description"));
						bundleSend.putString("latitude", ""+bundle.getString("latitude"));
						bundleSend.putString("longitude", ""+bundle.getString("longitude"));
						bundleSend.putString("rate", reportRate.getText().toString());
						bundleSend.putString("videoID", videoList[position]);
						intent.putExtras(bundleSend);
						startActivity(intent);
						overridePendingTransition(R.anim.fade, R.anim.hold);
						videoDialog.dismiss();
						finish();
					}
				});
				videoDialog.setTitle("Video List");
				videoDialog.setContentView(listView);
				progressDialog.dismiss();
				videoDialog.show();
			}
			else if(progress[0]==3) {
				progressDialog.dismiss();
				Toast.makeText(ReportView.this, getResources().getStringArray(R.array.noVideo)[SharedVariables.currentLanguage], Toast.LENGTH_LONG).show();
			}
		}

		@Override
		protected void onPostExecute(String result) {}
	}
}
